package com.tigercel.xiangchengyun.ui.activities;


import android.app.Fragment;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.manager.RequestManagerFragment;
import com.tigercel.xiangchengyun.R;

import cn.jzvd.JZVideoPlayerStandard;

public class VideoAndWebActivity extends AppCompatActivity {
    private JZVideoPlayerStandard mJZVideoPlayerStandard;
    private WebView mWebView;
    private String videoUrl;
    private String Url;
    private TextView title;
    private ProgressBar mProgressBar;
    private RelativeLayout noContent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_and_web);
        mJZVideoPlayerStandard = findViewById(R.id.videoplayer);
        videoUrl = getIntent().getStringExtra("videourl");

        Url = getIntent().getStringExtra("url");

        mJZVideoPlayerStandard.setUp(videoUrl, JZVideoPlayerStandard.SCREEN_WINDOW_NORMAL, "");
        mJZVideoPlayerStandard.startVideo();
        mWebView = findViewById(R.id.webView);
        mProgressBar = findViewById(R.id.progressbar);
        title = findViewById(R.id.titlebar_tv);
        noContent = findViewById(R.id.li_no_content);
//        title.setText(getIntent().getStringExtra("title"));
        if (Url.equals("")) {
            mWebView.setVisibility(View.GONE);
            noContent.setVisibility(View.VISIBLE);
            Fragment fa = new RequestManagerFragment();
            getFragmentManager().beginTransaction().replace(R.id.re_fragment, fa).commit();
        } else {
            initWebSettings();
            mProgressBar.setMax(100);
            mWebView.loadUrl(Url);
        }

    }

    private void initWebSettings() {
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setAppCacheEnabled(false);
        webSettings.setBlockNetworkImage(false);
        webSettings.setBuiltInZoomControls(false);
        webSettings.setSupportZoom(true);
        webSettings.setJavaScriptEnabled(true);
        webSettings.setDomStorageEnabled(true);
        webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        mWebView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                return false;
            }
        });
        mWebView.setWebViewClient(new WebViewClient() {

            @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                mWebView.loadUrl(request.getUrl().toString());
                return super.shouldOverrideUrlLoading(view, request);
            }
        });
        mWebView.setWebChromeClient(new WebChromeClient() {

            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                newProgress = mProgressBar.getProgress();
                if (newProgress == 100) {
                    mProgressBar.setVisibility(View.GONE);
                } else {
                    mProgressBar.setVisibility(View.VISIBLE);
                }
                super.onProgressChanged(view, newProgress);

            }
        });
    }

    @Override
    public void onBackPressed() {
        if (mJZVideoPlayerStandard.backPress()) {
            return;
        }
        super.onBackPressed();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mJZVideoPlayerStandard.releaseAllVideos();
    }
}
